// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev1.service.impl;

import java.util.Iterator;
import java.util.List;
import com.orange.boot.utils.ResultCode;
import com.orange.boot.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.orange.boot.utils.R;
import com.orange.boot.dev1.entity.UserVo;
import org.springframework.stereotype.Service;
import com.orange.boot.dev1.service.UserService;
import com.orange.boot.dev1.entity.User;
import com.orange.boot.dev1.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    public UserServiceImpl() {
    }

    public R login(UserVo userVo) {
        User userFromDB = (User)((UserMapper)this.baseMapper).selectOne((Wrapper)((QueryWrapper)(new QueryWrapper()).eq("username", userVo.getUsername())).eq("password", MD5.encrypt(userVo.getPassword())));
        return userFromDB == null ? R.error().message("用户名或密码错误") : R.ok().data("user", userFromDB);
    }

    public R register(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username", user.getUsername());
        User user1 = (User)((UserMapper)this.baseMapper).selectOne(wrapper);
        if (user1 != null) {
            return R.error().code(ResultCode.ALREADY_EXISTS).message("该账号已被注册！");
        } else {
            user.setPassword(MD5.encrypt(user.getPassword()));
            ((UserMapper)this.baseMapper).insert(user);
            return R.ok();
        }
    }

    public R updateUser(User user) {
        ((UserMapper)this.baseMapper).updateById(user);
        return R.ok();
    }

    public R listByStatus(Integer status) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", status);
        List<User> users = ((UserMapper)this.baseMapper).selectList(queryWrapper);
        Iterator var4 = users.iterator();

        while(var4.hasNext()) {
            User user = (User)var4.next();
            System.out.println(user);
        }

        return R.ok();
    }

    public R addUser(User user) {
        ((UserMapper)this.baseMapper).insert(user);
        return R.ok();
    }

    @Override
    public R detailInfo(UserVo userVo) {
        User userFromDB = baseMapper.selectOne(new QueryWrapper<User>()
                .eq("username", userVo.getUsername())
                .eq("password", MD5.encrypt(userVo.getPassword())));
        if (userFromDB == null) {
            return R.error().message("不存在该用户");
        }
        return R.ok().data("user", userFromDB);
    }

    @Override
    public R reviseInfo(User user) {
        try {
            // 检查用户是否存在
            if (user == null || user.getId() == null) {
                return R.error("用户信息不完整");
            }

            // 更新用户信息
            int updated = baseMapper.updateById(user);
            if (updated > 0){
                return R.ok();
            } else {
                return R.error("用户信息未更新");
            }
        } catch (Exception e) {
            // 记录日志或处理异常
            return R.error("更新用户信息时发生错误：" + e.getMessage());
        }
    }
}
